layui.use(['form', 'table', 'layer'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    loadData(table);

    // 监听搜索操作
    form.on('submit(data-search-btn-cloudtag)', function (data) {
        //执行搜索重载
        table.reload('cloudTag-info-list', {
            page: {curr: 1},//重新从第 1 页开始
            where: data.field
        });

        return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(cloudTag-info-list-filter)', function (obj) {
        if (obj.event === 'add') {  // 监听添加操作
            addCloudTag(table, form);
        }

        if (obj.event === 'edit') {  // 监听添加操作
            editCloudTag(table, form);
        }

        if (obj.event === 'delete') {  // 监听删除操作
            deleteTag(table);
        }
    });

    //监听表格复选框选择
    table.on('checkbox(cloudTag-info-list-filter)', function (obj) {
        var cacheElement = table.cache['cloudTag-info-list'];
        tableCheckBoxOnly(obj, cacheElement);
    });

});

function deleteTag(table) {
    var selectdata = table.checkStatus('cloudTag-info-list').data;
    if (selectdata.length === 0) {
        lay_tishi("选择一条数据");
        return false;
    }
    if (selectdata.length > 1) {
        lay_tishi("有多条数据");
        return false;
    }
    layer.confirm('确认删除', function (index) {
        const param = 'id=' + selectdata[0].id;
        var resultData = JSONAuthAjax(dl_coder_url.delete_cloud_tag[0], dl_coder_url.delete_cloud_tag[1], param);

        if (resultData.code === '200') {
            loadData(table);
            layer.close(index);
        }
    });
}

function addCloudTag(table, form) {
    $("#id").val("");
    $("#tagTitle").val("");
    $("#tagValue").val("");
    $("#tagAlert").val("");
    layer.open({
        type: 1,
        title: "新增标签",
        area: ['50%', '50%'],
        btn: ['确定', '取消'],
        content: $('#cloudTag-update-add-from'),
        yes: function (index, layero) {
            layui.use(['form'], function () {
                init_update_cloudtag(form, index, '1', table);
            });
        }
    });

}

function editCloudTag(table, form) {
    var selectCloudTagdata = table.checkStatus('cloudTag-info-list').data;
    if (selectCloudTagdata === null || selectCloudTagdata.length === 0) {
        lay_tishi("选择一条数据");
        return false;
    }
    if (selectCloudTagdata.length > 1) {
        lay_tishi("有多条数据");
        return false;
    }

    $("#id").val(selectCloudTagdata[0].id);
    $("#tagTitle").val(selectCloudTagdata[0].tagTitle);
    $("#tagValue").val(selectCloudTagdata[0].tagValue);
    $("#tagAlert").val(selectCloudTagdata[0].tagAlert);
    layer.open({
        type: 1,
        title: "修改标签",
        area: ['50%', '50%'],
        btn: ['确定', '取消'],
        content: $('#cloudTag-update-add-from'),
        yes: function (index, layero) {
            layui.use(['form'], function () {
                init_update_cloudtag(form, index, '2', table);
            });
        }
    });
}

function init_update_cloudtag(form, index, type, table) {
    form.render();
    if (!form.validate('#cloudTag-update-add-from')) {
        return false;
    }

    const dataFormValue = $("#cloudTag-update-add-from").serializeJson();
    var resultData;
    //新增
    if (type === "1") {
        resultData = JSONAuthAjax(dl_coder_url.add_cloud_tag[0], dl_coder_url.add_cloud_tag[1], dataFormValue);
    } else {
        //修改
        resultData = JSONAuthAjax(dl_coder_url.edit_cloud_tag[0], dl_coder_url.edit_cloud_tag[1], dataFormValue);
    }
    if (resultData.code === '200') {
        loadData(table);
        layer.close(index);
    }
}


function loadData(table, where) {
    table.render({
        elem: '#cloudTag-info-list',
        url: dl_coder_url.cloud_tag_page[0],
        toolbar: '#cloudTag-list-toolbar',
        method: dl_coder_url.cloud_tag_page[1],
        where: where,
        contentType: 'application/json',
        dataType: 'json',
        request: {
            pageName: 'pageNo', //页码的参数名称，默认：page
            limitName: 'pageSize', //每页数据量的参数名，默认：limit
        },
        headers: headerMap,
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "total": res.data.total, //解析数据长度
                "data": res.data.records, //解析数据列表
                "count": res.data.pages, //解析数据长度
            }
        },
        response: {
            statusName: 'code', //规定数据状态的字段名称，默认：code
            statusCode: 200,//规定成功的状态码，默认：0
            msgName: 'msg', //规定状态信息的字段名称，默认：msg
            countName: 'total', //规定数据总数的字段名称，默认：count
            dataName: 'data', //规定数据列表的字段名称，默认：data
        },
        cols: [[
            {type: "checkbox"},
            {field: 'id',align: 'center', width: 80, title: 'ID', sort: true},
            {field: 'tagTitle',align: 'center', width: 200, title: '标签标题'},
            {field: 'tagAlert',align: 'center', width: 200, title: '标签注释'},
            {field: 'tagValue',align: 'center', title: '标签描述'},
            {field: 'createTime',align: 'center', width: 200, title: '添加时间'},
            {field: 'updateTime',align: 'center', width: 200, title: '修改时间'}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true,
        skin: 'line',
        even: true,
        size: 'lg',
        height: 'full-105' ,
        done: function () {
            setAuthority();
        }
    });
}